{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import division, print_function, absolute_import\n",
    "\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "HEIGHT = 32\n",
    "WIDTH = 32\n",
    "DEPTH = 3\n",
    "\n",
    "\n",
    "def parser(record):\n",
    "    keys_to_features={\n",
    "        'image': tf.FixedLenFeature((), tf.string),\n",
    "        'label': tf.FixedLenFeature((), tf.int64)\n",
    "    }\n",
    "    parsed = tf.io.parse_single_example(record, features=keys_to_features)\n",
    "    \n",
    "    image = tf.decode_raw(parsed['image'], tf.uint8)\n",
    "    image.set_shape([DEPTH * HEIGHT * WIDTH])\n",
    "    # Reshape from [depth * height * width] to [depth, height, width].\n",
    "    image = tf.cast(tf.transpose(tf.reshape(image, [DEPTH, HEIGHT, WIDTH]), [1, 2, 0]), tf.float32)\n",
    "    \n",
    "    label = tf.cast(parsed['label'], tf.int32)\n",
    "    \n",
    "    return image, label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_input_fn(params):\n",
    "    train_dataset = tf.data.TFRecordDataset('../data/train.tfrecord')\n",
    "    train_dataset = train_dataset.map(parser)\n",
    "    train_dataset = train_dataset.repeat()\n",
    "    train_dataset = train_dataset.batch(params['batch_size'])\n",
    "    \n",
    "    train_iterator = train_dataset.make_one_shot_iterator()\n",
    "    features, labels = train_iterator.get_next()\n",
    "    \n",
    "    return features, labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[100  32  32   3]\n"
     ]
    }
   ],
   "source": [
    "# Training Parameters\n",
    "params = {}\n",
    "params['num_epochs'] = 100\n",
    "params['learning_rate'] = 0.01\n",
    "params['num_steps'] = 2000\n",
    "params['batch_size'] = 100\n",
    "params['dropout_rate'] = 0.75\n",
    "\n",
    "features, labels = train_input_fn(params)\n",
    "with tf.Session() as sess:\n",
    "    print(sess.run(tf.shape(features)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
